


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>TorchScript &mdash; PyTorch master documentation</title>
  

  
  
  
  
    <link rel="canonical" href="https://pytorch.org/docs/stable/jit.html"/>
  

  

  
  
    

  

  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <!-- <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css" type="text/css" />
  <link rel="stylesheet" href="_static/css/jit.css" type="text/css" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" type="text/css" />
  <link rel="stylesheet" href="_static/katex-math.css" type="text/css" />
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="TorchScript Builtins" href="jit_builtin_functions.html" />
    <link rel="prev" title="torch.hub" href="hub.html" /> 

  
  <script src="_static/js/modernizr.min.js"></script>

  <!-- Preload the theme fonts -->

<link rel="preload" href="_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">

<!-- Preload the katex fonts -->

<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
</head>

<div class="container-fluid header-holder tutorials-header" id="header-holder">
  <div class="container">
    <div class="header-container">
      <a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>

      <div class="main-menu">
        <ul>
          <li>
            <a href="https://pytorch.org/get-started">Get Started</a>
          </li>

          <li>
            <div class="ecosystem-dropdown">
              <a id="dropdownMenuButton" data-toggle="ecosystem-dropdown">
                Ecosystem
              </a>
              <div class="ecosystem-dropdown-menu">
                <a class="nav-dropdown-item" href="https://pytorch.org/hub"">
                  <span class=dropdown-title>Models (Beta)</span>
                  <p>Discover, publish, and reuse pre-trained models</p>
                </a>
                <a class="nav-dropdown-item" href="https://pytorch.org/ecosystem">
                  <span class=dropdown-title>Tools & Libraries</span>
                  <p>Explore the ecosystem of tools and libraries</p>
                </a>
              </div>
            </div>
          </li>

          <li>
            <a href="https://pytorch.org/mobile">Mobile</a>
          </li>

          <li>
            <a href="https://pytorch.org/blog/">Blog</a>
          </li>

          <li>
            <a href="https://pytorch.org/tutorials">Tutorials</a>
          </li>

          <li class="active">
            <a href="https://pytorch.org/docs/stable/index.html">Docs</a>
          </li>

          <li>
            <div class="resources-dropdown">
              <a id="resourcesDropdownButton" data-toggle="resources-dropdown">
                Resources
              </a>
              <div class="resources-dropdown-menu">
                <a class="nav-dropdown-item" href="https://pytorch.org/resources"">
                  <span class=dropdown-title>Developer Resources</span>
                  <p>Find resources and get questions answered</p>
                </a>
                <a class="nav-dropdown-item" href="https://pytorch.org/features">
                  <span class=dropdown-title>About</span>
                  <p>Learn about PyTorch’s features and capabilities</p>
                </a>
              </div>
            </div>
          </li>

          <li>
            <a href="https://github.com/pytorch/pytorch">Github</a>
          </li>
        </ul>
      </div>

      <a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
    </div>

  </div>
</div>


<body class="pytorch-body">

   

    

    <div class="table-of-contents-link-wrapper">
      <span>Table of Contents</span>
      <a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
    </div>

    <nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
      <div class="pytorch-side-scroll">
        <div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          <div class="pytorch-left-menu-search">
            

            
              
              
                <div class="version">
                  master (1.5.0 )
                </div>
              
            

            


  


<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search Docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

            
          </div>

          
<div>
  <a style="color:#F05732" href="https://pytorch.org/docs/stable/jit.html">
    You are viewing unstable developer preview docs.
    Click here to view docs for latest stable release.
  </a>
</div>

            
            
              
            
            
              <p class="caption"><span class="caption-text">Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="notes/amp_examples.html">Automatic Mixed Precision examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/autograd.html">Autograd mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/broadcasting.html">Broadcasting semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cpu_threading_torchscript_inference.html">CPU threading and TorchScript inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cuda.html">CUDA semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/ddp.html">Distributed Data Parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/extending.html">Extending PyTorch</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/large_scale_deployments.html">Features for large-scale deployments</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption"><span class="caption-text">Language Bindings</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/cppdocs/">C++ API</a></li>
<li class="toctree-l1"><a class="reference internal" href="packages.html">Javadoc</a></li>
</ul>
<p class="caption"><span class="caption-text">Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="torch.html">torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.functional.html">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensors.html">torch.Tensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_attributes.html">Tensor Attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_view.html">Tensor Views</a></li>
<li class="toctree-l1"><a class="reference internal" href="autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="cuda.html">torch.cuda</a></li>
<li class="toctree-l1"><a class="reference internal" href="amp.html">torch.cuda.amp</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">torch.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributions.html">torch.distributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="hub.html">torch.hub</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.init.html">torch.nn.init</a></li>
<li class="toctree-l1"><a class="reference internal" href="onnx.html">torch.onnx</a></li>
<li class="toctree-l1"><a class="reference internal" href="optim.html">torch.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="quantization.html">Quantization</a></li>
<li class="toctree-l1"><a class="reference internal" href="rpc/index.html">Distributed RPC Framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="random.html">torch.random</a></li>
<li class="toctree-l1"><a class="reference internal" href="sparse.html">torch.sparse</a></li>
<li class="toctree-l1"><a class="reference internal" href="storage.html">torch.Storage</a></li>
<li class="toctree-l1"><a class="reference internal" href="bottleneck.html">torch.utils.bottleneck</a></li>
<li class="toctree-l1"><a class="reference internal" href="checkpoint.html">torch.utils.checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="cpp_extension.html">torch.utils.cpp_extension</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">torch.utils.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="dlpack.html">torch.utils.dlpack</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_zoo.html">torch.utils.model_zoo</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensorboard.html">torch.utils.tensorboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="type_info.html">Type Info</a></li>
<li class="toctree-l1"><a class="reference internal" href="named_tensor.html">Named Tensors</a></li>
<li class="toctree-l1"><a class="reference internal" href="name_inference.html">Named Tensors operator coverage</a></li>
<li class="toctree-l1"><a class="reference internal" href="__config__.html">torch.__config__</a></li>
</ul>
<p class="caption"><span class="caption-text">Libraries</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/audio">torchaudio</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/text">torchtext</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/elastic/">TorchElastic</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/serve">TorchServe</a></li>
<li class="toctree-l1"><a class="reference external" href="http://pytorch.org/xla/">PyTorch on XLA Devices</a></li>
</ul>
<p class="caption"><span class="caption-text">Community</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="community/contribution_guide.html">PyTorch Contribution Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/governance.html">PyTorch Governance</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/persons_of_interest.html">PyTorch Governance | Persons of Interest</a></li>
</ul>

            
          

        </div>
      </div>
    </nav>

    <div class="pytorch-container">
      <div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
        <div class="pytorch-breadcrumbs-wrapper">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="pytorch-breadcrumbs">
    
      <li>
        <a href="index.html">
          
            Docs
          
        </a> &gt;
      </li>

        
      <li>TorchScript</li>
    
    
      <li class="pytorch-breadcrumbs-aside">
        
            
            <a href="_sources/jit.rst.txt" rel="nofollow"><img src="_static/images/view-page-source-icon.svg"></a>
          
        
      </li>
    
  </ul>

  
</div>
        </div>

        <div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
          Shortcuts
        </div>
      </div>

      <section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
        <div class="pytorch-content-left">

        
          
          <div class="rst-content">
          
            <div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
             <article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
              
  <div class="section" id="torchscript">
<h1>TorchScript<a class="headerlink" href="#torchscript" title="Permalink to this headline">¶</a></h1>
<div class="toctree-wrapper compound">
</div>
<div class="toctree-wrapper compound">
</div>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#creating-torchscript-code" id="id4">Creating TorchScript Code</a></p></li>
<li><p><a class="reference internal" href="#mixing-tracing-and-scripting" id="id5">Mixing Tracing and Scripting</a></p></li>
<li><p><a class="reference internal" href="#torchscript-language" id="id6">TorchScript Language</a></p></li>
<li><p><a class="reference internal" href="#built-in-functions-and-modules" id="id7">Built-in Functions and Modules</a></p>
<ul>
<li><p><a class="reference internal" href="#pytorch-functions-and-modules" id="id8">PyTorch Functions and Modules</a></p></li>
<li><p><a class="reference internal" href="#python-functions-and-modules" id="id9">Python Functions and Modules</a></p></li>
<li><p><a class="reference internal" href="#python-language-reference-comparison" id="id10">Python Language Reference Comparison</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#debugging" id="id11">Debugging</a></p>
<ul>
<li><p><a class="reference internal" href="#disable-jit-for-debugging" id="id12">Disable JIT for Debugging</a></p></li>
<li><p><a class="reference internal" href="#inspecting-code" id="id13">Inspecting Code</a></p></li>
<li><p><a class="reference internal" href="#interpreting-graphs" id="id14">Interpreting Graphs</a></p></li>
<li><p><a class="reference internal" href="#tracer" id="id15">Tracer</a></p></li>
<li><p><a class="reference internal" href="#id1" id="id16">Built-in Functions and Modules</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#frequently-asked-questions" id="id17">Frequently Asked Questions</a></p></li>
<li><p><a class="reference internal" href="#appendix" id="id18">Appendix</a></p>
<ul>
<li><p><a class="reference internal" href="#migrating-to-pytorch-1-2-recursive-scripting-api" id="id19">Migrating to PyTorch 1.2 Recursive Scripting API</a></p></li>
</ul>
</li>
</ul>
</div>
<span class="target" id="module-torch.jit"></span><p>TorchScript is a way to create serializable and optimizable models from PyTorch code.
Any TorchScript program can be saved from a Python
process and loaded in a process where there is no Python dependency.</p>
<p>We provide tools to incrementally transition a model from a pure Python program
to a TorchScript program that can be run independently from Python, such as in a standalone C++ program.
This makes it possible to train models in PyTorch using familiar tools in Python and then export
the model via TorchScript to a production environment where Python programs may be disadvantageous
for performance and multi-threading reasons.</p>
<p>For a gentle introduction to TorchScript, see the <a class="reference external" href="https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html">Introduction to TorchScript</a> tutorial.</p>
<p>For an end-to-end example of converting a PyTorch model to TorchScript and running it in C++, see the
<a class="reference external" href="https://pytorch.org/tutorials/advanced/cpp_export.html">Loading a PyTorch Model in C++</a> tutorial.</p>
<div class="section" id="creating-torchscript-code">
<h2><a class="toc-backref" href="#id4">Creating TorchScript Code</a><a class="headerlink" href="#creating-torchscript-code" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="torch.jit.script">
<code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">script</code><span class="sig-paren">(</span><em class="sig-param">obj</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/jit.html#script"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.script" title="Permalink to this definition">¶</a></dt>
<dd><p>Scripting a function or <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code> will inspect the source code, compile
it as TorchScript code using the TorchScript compiler, and return a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> or
<a class="reference internal" href="#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a>. TorchScript itself is a subset of the Python language, so not all
features in Python work, but we provide enough functionality to compute on
tensors and do control-dependent operations. For a complete guide, see the
<a class="reference internal" href="jit_language_reference.html#language-reference"><span class="std std-ref">TorchScript Language Reference</span></a>.</p>
<p><code class="docutils literal notranslate"><span class="pre">torch.jit.script</span></code> can be used as a function for modules and functions, and as a decorator
<code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.script</span></code> for <a class="reference internal" href="jit_language_reference.html#id2"><span class="std std-ref">TorchScript Classes</span></a> and functions.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>obj</strong> (callable, class, or <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>) – The <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>, function, or class type to
compile.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>, <code class="docutils literal notranslate"><span class="pre">script</span></code> returns
a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> object. The returned <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> will
have the same set of sub-modules and parameters as the
original <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>. If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a standalone function,
a <a class="reference internal" href="#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a> will be returned.</p>
</dd>
</dl>
<dl>
<dt><strong>Scripting a function</strong></dt><dd><p>The <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.script</span></code> decorator will construct a <a class="reference internal" href="#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a>
by compiling the body of the function.</p>
<p>Example (scripting a function):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">y</span><span class="o">.</span><span class="n">max</span><span class="p">():</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">x</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">y</span>
    <span class="k">return</span> <span class="n">r</span>

<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">foo</span><span class="p">))</span>  <span class="c1"># torch.jit.ScriptFuncion</span>

<span class="c1"># See the compiled graph as Python code</span>
<span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

<span class="c1"># Call the function using the TorchScript interpreter</span>
<span class="n">foo</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
</pre></div>
</div>
</dd>
<dt><strong>Scripting an nn.Module</strong></dt><dd><p>Scripting an <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code> by default will compile the <code class="docutils literal notranslate"><span class="pre">forward</span></code> method and recursively
compile any methods, submodules, and functions called by <code class="docutils literal notranslate"><span class="pre">forward</span></code>. If a <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code> only uses
features supported in TorchScript, no changes to the original module code should be necessary. <code class="docutils literal notranslate"><span class="pre">script</span></code>
will construct <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> that has copies of the attributes, parameters, and methods of
the original module.</p>
<p>Example (scripting a simple module with a Parameter):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="n">M</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="c1"># This parameter will be copied to the new ScriptModule</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">,</span> <span class="n">M</span><span class="p">))</span>

        <span class="c1"># When this submodule is used, it will be compiled</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">linear</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="n">N</span><span class="p">,</span> <span class="n">M</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">mv</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>

        <span class="c1"># This calls the `forward` method of the `nn.Linear` module, which will</span>
        <span class="c1"># cause the `self.linear` submodule to be compiled to a `ScriptModule` here</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">linear</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span>

<span class="n">scripted_module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
</pre></div>
</div>
<p>Example (scripting a module with traced submodules):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="kn">import</span> <span class="nn">torch.nn.functional</span> <span class="k">as</span> <span class="nn">F</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="c1"># torch.jit.trace produces a ScriptModule&#39;s conv1 and conv2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">16</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv2</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">16</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
      <span class="nb">input</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">conv1</span><span class="p">(</span><span class="nb">input</span><span class="p">))</span>
      <span class="nb">input</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">conv2</span><span class="p">(</span><span class="nb">input</span><span class="p">))</span>
      <span class="k">return</span> <span class="nb">input</span>

<span class="n">scripted_module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>
</pre></div>
</div>
<p>To compile a method other than <code class="docutils literal notranslate"><span class="pre">forward</span></code> (and recursively compile anything it calls), add
the <a class="reference internal" href="#torch.jit.export" title="torch.jit.export"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.export</span></code></a> decorator to the method. To opt out of compilation
use <a class="reference internal" href="#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code></a> or <a class="reference internal" href="#torch.jit.unused" title="torch.jit.unused"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.unused</span></code></a>.</p>
<p>Example (an exported and ignored method in a module):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

    <span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">export</span>
    <span class="k">def</span> <span class="nf">some_entry_point</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">input</span> <span class="o">+</span> <span class="mi">10</span>

    <span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">ignore</span>
    <span class="k">def</span> <span class="nf">python_only_fn</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="c1"># This function won&#39;t be compiled, so any</span>
        <span class="c1"># Python APIs can be used</span>
        <span class="kn">import</span> <span class="nn">pdb</span>
        <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">training</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">python_only_fn</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">input</span> <span class="o">*</span> <span class="mi">99</span>

<span class="n">scripted_module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">scripted_module</span><span class="o">.</span><span class="n">some_entry_point</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">scripted_module</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)))</span>
</pre></div>
</div>
</dd>
</dl>
</dd></dl>

<dl class="function">
<dt id="torch.jit.trace">
<code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">trace</code><span class="sig-paren">(</span><em class="sig-param">func</em>, <em class="sig-param">example_inputs</em>, <em class="sig-param">optimize=None</em>, <em class="sig-param">check_trace=True</em>, <em class="sig-param">check_inputs=None</em>, <em class="sig-param">check_tolerance=1e-5</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/jit.html#trace"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.trace" title="Permalink to this definition">¶</a></dt>
<dd><p>Trace a function and return an executable  or <a class="reference internal" href="#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a>
that will be optimized using just-in-time compilation. Tracing is ideal for
code that operates only on <code class="docutils literal notranslate"><span class="pre">Tensor</span></code>s and lists, dictionaries, and tuples of <code class="docutils literal notranslate"><span class="pre">Tensor</span></code>s.</p>
<p>Using <code class="docutils literal notranslate"><span class="pre">torch.jit.trace</span></code> and <a class="reference internal" href="#torch.jit.trace_module" title="torch.jit.trace_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.trace_module</span></code></a>, you can turn an existing module or Python
function into a TorchScript <a class="reference internal" href="#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a> or <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>. You must provide example inputs,
and we run the function, recording the operations performed on all the tensors.</p>
<ul class="simple">
<li><p>The resulting recording of a standalone function produces <a class="reference internal" href="#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a>.</p></li>
<li><p>The resulting recording of <code class="docutils literal notranslate"><span class="pre">forward</span></code> function of <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code> or <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code> produces <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>.</p></li>
</ul>
<p>This module also contains any parameters that the original
module had as well.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Tracing only correctly records functions and modules which are not data
dependent (e.g., do not have conditionals on data in tensors) and do not have
any untracked external dependencies (e.g., perform input/output or
access global variables). Tracing only records operations done when the given
function is run on the given
tensors. Therefore, the returned <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> will always run the same traced
graph on any input. This has some important implications when your module is
expected to run different sets of operations, depending on the input and/or the
module state. For example,</p>
<ul class="simple">
<li><p>Tracing will not record any control-flow like if-statements or loops.
When this control-flow is constant across your module, this is fine and it often
inlines the control-flow decisions. But sometimes the control-flow is actually part
of the model itself. For instance, a recurrent network is a loop over
the (possibly dynamic) length of an input sequence.</p></li>
<li><p>In the returned <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>, operations that have different
behaviors in <code class="docutils literal notranslate"><span class="pre">training</span></code> and <code class="docutils literal notranslate"><span class="pre">eval</span></code> modes will always behave as if it
is in the mode it was in during tracing, no matter which mode the
<a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> is in.</p></li>
</ul>
<p>In cases like these, tracing would not be appropriate and <a class="reference internal" href="#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">scripting</span></code></a> is a better
choice. If you trace such models, you may silently get
incorrect results on subsequent invocations of the model. The tracer
will try to emit warnings when doing something that may cause an
incorrect trace to be produced.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>func</strong> (<em>callable</em><em> or </em><a class="reference internal" href="nn.html#torch.nn.Module" title="torch.nn.Module"><em>torch.nn.Module</em></a>) – A Python function or <code class="docutils literal notranslate"><span class="pre">torch.nn.Module</span></code>
that will be run with <code class="docutils literal notranslate"><span class="pre">example_inputs</span></code>.
arguments and returns to <code class="docutils literal notranslate"><span class="pre">func</span></code> must be tensors
or (possibly nested) tuples that
contain tensors. When a module is passed to
<a class="reference internal" href="#torch.jit.trace" title="torch.jit.trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.trace</span></code></a>, only the
<code class="docutils literal notranslate"><span class="pre">forward</span></code> method is run and traced
(see <a class="reference internal" href="#torch.jit.trace_module" title="torch.jit.trace_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.trace</span></code></a> for details).</p></li>
<li><p><strong>example_inputs</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.8)"><em>tuple</em></a>) – A tuple of example inputs that will be passed to the function
while tracing. The resulting trace can be run with
inputs of different types and shapes assuming the traced operations
support those types and shapes. <code class="docutils literal notranslate"><span class="pre">example_inputs</span></code> may also be a single
Tensor in which case it is automatically wrapped in a tuple.</p></li>
</ul>
</dd>
<dt class="field-even">Keyword Arguments</dt>
<dd class="field-even"><ul class="simple">
<li><p><strong>check_trace</strong> (<a class="reference internal" href="storage.html#torch.FloatStorage.bool" title="torch.FloatStorage.bool"><em>bool</em></a><em>, </em><em>optional</em>) – Check if the same inputs run through
traced code produce the same outputs. Default: <code class="docutils literal notranslate"><span class="pre">True</span></code>. You might want
to disable this if, for example, your network contains non-
deterministic ops or if you are sure that the network is correct despite
a checker failure.</p></li>
<li><p><strong>check_inputs</strong> (<em>list of tuples</em><em>, </em><em>optional</em>) – A list of tuples of input arguments that should be used
to check the trace against what is expected. Each tuple
is equivalent to a set of input arguments that would
be specified in <code class="docutils literal notranslate"><span class="pre">example_inputs</span></code>. For best results, pass in a
set of checking inputs representative of the space of
shapes and types of inputs you expect the network to see.
If not specified, the original <code class="docutils literal notranslate"><span class="pre">example_inputs</span></code> are used for checking</p></li>
<li><p><strong>check_tolerance</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a><em>, </em><em>optional</em>) – Floating-point comparison tolerance to use in the checker procedure.
This can be used to relax the checker strictness in the event that
results diverge numerically for a known reason, such as operator fusion.</p></li>
</ul>
</dd>
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>If <code class="docutils literal notranslate"><span class="pre">callable</span></code> is <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code> or <code class="docutils literal notranslate"><span class="pre">forward</span></code> of <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>, <code class="docutils literal notranslate"><span class="pre">trace</span></code> returns
a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> object with a single <code class="docutils literal notranslate"><span class="pre">forward</span></code> method containing the traced code.
The returned <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> will have the same set of sub-modules and parameters as the
original <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>.
If <code class="docutils literal notranslate"><span class="pre">callable</span></code> is a standalone function, <code class="docutils literal notranslate"><span class="pre">trace</span></code> returns <a class="reference internal" href="#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a></p>
</dd>
</dl>
<p>Example (tracing a function):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>

<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
    <span class="k">return</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>

<span class="c1"># Run `foo` with the provided inputs and record the tensor operations</span>
<span class="n">traced_foo</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">foo</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</span>

<span class="c1"># `traced_foo` can now be run with the TorchScript interpreter or saved</span>
<span class="c1"># and loaded in a Python-free environment</span>
</pre></div>
</div>
<p>Example (tracing an existing module):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="k">class</span> <span class="nc">Net</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

<span class="n">n</span> <span class="o">=</span> <span class="n">Net</span><span class="p">()</span>
<span class="n">example_weight</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">example_forward_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>

<span class="c1"># Trace a specific method and construct `ScriptModule` with</span>
<span class="c1"># a single `forward` method</span>
<span class="n">module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">n</span><span class="o">.</span><span class="n">forward</span><span class="p">,</span> <span class="n">example_forward_input</span><span class="p">)</span>

<span class="c1"># Trace a module (implicitly traces `forward`) and construct a</span>
<span class="c1"># `ScriptModule` with a single `forward` method</span>
<span class="n">module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">example_forward_input</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="torch.jit.trace_module">
<code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">trace_module</code><span class="sig-paren">(</span><em class="sig-param">mod</em>, <em class="sig-param">inputs</em>, <em class="sig-param">optimize=None</em>, <em class="sig-param">check_trace=True</em>, <em class="sig-param">check_inputs=None</em>, <em class="sig-param">check_tolerance=1e-5</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/jit.html#trace_module"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.trace_module" title="Permalink to this definition">¶</a></dt>
<dd><p>Trace a module and return an executable <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> that will be optimized
using just-in-time compilation. When a module is passed to <a class="reference internal" href="#torch.jit.trace" title="torch.jit.trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.trace</span></code></a>, only
the <code class="docutils literal notranslate"><span class="pre">forward</span></code> method is run and traced. With <code class="docutils literal notranslate"><span class="pre">trace_module</span></code>, you can specify a dictionary of
method names to example inputs to trace (see the <code class="docutils literal notranslate"><span class="pre">example_inputs</span></code>) argument below.</p>
<p>See <a class="reference internal" href="#torch.jit.trace" title="torch.jit.trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.trace</span></code></a> for more information on tracing.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>mod</strong> (<a class="reference internal" href="nn.html#torch.nn.Module" title="torch.nn.Module"><em>torch.nn.Module</em></a>) – A <code class="docutils literal notranslate"><span class="pre">torch.nn.Module</span></code> containing methods whose names are
specified in <code class="docutils literal notranslate"><span class="pre">example_inputs</span></code>. The given methods will be compiled
as a part of a single <cite>ScriptModule</cite>.</p></li>
<li><p><strong>example_inputs</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.8)"><em>dict</em></a>) – A dict containing sample inputs indexed by method names in <code class="docutils literal notranslate"><span class="pre">mod</span></code>.
The inputs will be passed to methods whose names correspond to inputs’
keys while tracing.
<code class="docutils literal notranslate"><span class="pre">{</span> <span class="pre">'forward'</span> <span class="pre">:</span> <span class="pre">example_forward_input,</span> <span class="pre">'method2':</span> <span class="pre">example_method2_input}</span></code></p></li>
</ul>
</dd>
<dt class="field-even">Keyword Arguments</dt>
<dd class="field-even"><ul class="simple">
<li><p><strong>check_trace</strong> (<code class="docutils literal notranslate"><span class="pre">bool</span></code>, optional) – Check if the same inputs run through
traced code produce the same outputs. Default: <code class="docutils literal notranslate"><span class="pre">True</span></code>. You might want
to disable this if, for example, your network contains non-
deterministic ops or if you are sure that the network is correct despite
a checker failure.</p></li>
<li><p><strong>check_inputs</strong> (<em>list of dicts</em><em>, </em><em>optional</em>) – A list of dicts of input arguments that should be used
to check the trace against what is expected. Each tuple
is equivalent to a set of input arguments that would
be specified in <code class="docutils literal notranslate"><span class="pre">example_inputs</span></code>. For best results, pass in a
set of checking inputs representative of the space of
shapes and types of inputs you expect the network to see.
If not specified, the original <code class="docutils literal notranslate"><span class="pre">example_inputs</span></code> are used for checking</p></li>
<li><p><strong>check_tolerance</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a><em>, </em><em>optional</em>) – Floating-point comparison tolerance to use in the checker procedure.
This can be used to relax the checker strictness in the event that
results diverge numerically for a known reason, such as operator fusion.</p></li>
</ul>
</dd>
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>A <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> object with a single <code class="docutils literal notranslate"><span class="pre">forward</span></code> method containing the traced code.
When <code class="docutils literal notranslate"><span class="pre">func</span></code> is a <code class="docutils literal notranslate"><span class="pre">torch.nn.Module</span></code>, the returned <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> will have the same set of
sub-modules and parameters as <code class="docutils literal notranslate"><span class="pre">func</span></code>.</p>
</dd>
</dl>
<p>Example (tracing a module with multiple methods):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="k">class</span> <span class="nc">Net</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">weighted_kernel_sum</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">weight</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">weight</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">weight</span>


<span class="n">n</span> <span class="o">=</span> <span class="n">Net</span><span class="p">()</span>
<span class="n">example_weight</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">example_forward_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>

<span class="c1"># Trace a specific method and construct `ScriptModule` with</span>
<span class="c1"># a single `forward` method</span>
<span class="n">module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">n</span><span class="o">.</span><span class="n">forward</span><span class="p">,</span> <span class="n">example_forward_input</span><span class="p">)</span>

<span class="c1"># Trace a module (implicitly traces `forward`) and construct a</span>
<span class="c1"># `ScriptModule` with a single `forward` method</span>
<span class="n">module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">example_forward_input</span><span class="p">)</span>

<span class="c1"># Trace specific methods on a module (specified in `inputs`), constructs</span>
<span class="c1"># a `ScriptModule` with `forward` and `weighted_kernel_sum` methods</span>
<span class="n">inputs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;forward&#39;</span> <span class="p">:</span> <span class="n">example_forward_input</span><span class="p">,</span> <span class="s1">&#39;weighted_kernel_sum&#39;</span> <span class="p">:</span> <span class="n">example_weight</span><span class="p">}</span>
<span class="n">module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace_module</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">inputs</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="torch.jit.ScriptModule">
<em class="property">class </em><code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">ScriptModule</code><a class="reference internal" href="_modules/torch/jit.html#ScriptModule"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.ScriptModule" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">ScriptModule``s</span> <span class="pre">wrap</span> <span class="pre">a</span> <span class="pre">C++</span> <span class="pre">``torch::jit::Module</span></code>. <code class="docutils literal notranslate"><span class="pre">ScriptModule``s</span>
<span class="pre">contain</span> <span class="pre">methods,</span> <span class="pre">attributes,</span> <span class="pre">parameters,</span> <span class="pre">and</span>
<span class="pre">constants.</span> <span class="pre">These</span> <span class="pre">can</span> <span class="pre">be</span> <span class="pre">accessed</span> <span class="pre">the</span> <span class="pre">same</span> <span class="pre">as</span> <span class="pre">on</span> <span class="pre">a</span> <span class="pre">normal</span> <span class="pre">``nn.Module</span></code>.</p>
<dl class="method">
<dt id="torch.jit.ScriptModule.code">
<em class="property">property </em><code class="sig-name descname">code</code><a class="headerlink" href="#torch.jit.ScriptModule.code" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a pretty-printed representation (as valid Python syntax) of
the internal graph for the <code class="docutils literal notranslate"><span class="pre">forward</span></code> method. See <a class="reference internal" href="#inspecting-code">Inspecting Code</a>
for details.</p>
</dd></dl>

<dl class="method">
<dt id="torch.jit.ScriptModule.graph">
<em class="property">property </em><code class="sig-name descname">graph</code><a class="headerlink" href="#torch.jit.ScriptModule.graph" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a string representation of the internal graph for the
<code class="docutils literal notranslate"><span class="pre">forward</span></code> method. See <a class="reference internal" href="#interpreting-graphs">Interpreting Graphs</a> for details.</p>
</dd></dl>

<dl class="method">
<dt id="torch.jit.ScriptModule.inlined_graph">
<em class="property">property </em><code class="sig-name descname">inlined_graph</code><a class="headerlink" href="#torch.jit.ScriptModule.inlined_graph" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a string representation of the internal graph for the
<code class="docutils literal notranslate"><span class="pre">forward</span></code> method. This graph will be preprocessed to inline all function and method calls.
See <a class="reference internal" href="#interpreting-graphs">Interpreting Graphs</a> for details.</p>
</dd></dl>

<dl class="method">
<dt id="torch.jit.ScriptModule.save">
<code class="sig-name descname">save</code><span class="sig-paren">(</span><em class="sig-param">f</em>, <em class="sig-param">_extra_files=ExtraFilesMap{}</em><span class="sig-paren">)</span><a class="headerlink" href="#torch.jit.ScriptModule.save" title="Permalink to this definition">¶</a></dt>
<dd><p>See <a class="reference internal" href="#torch.jit.save" title="torch.jit.save"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.save</span></code></a> for details.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="torch.jit.ScriptFunction">
<em class="property">class </em><code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">ScriptFunction</code><a class="headerlink" href="#torch.jit.ScriptFunction" title="Permalink to this definition">¶</a></dt>
<dd><p>Functionally equivalent to a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>, but represents a single
function and does not have any attributes or Parameters.</p>
</dd></dl>

<dl class="function">
<dt id="torch.jit.save">
<code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">save</code><span class="sig-paren">(</span><em class="sig-param">m</em>, <em class="sig-param">f</em>, <em class="sig-param">_extra_files=ExtraFilesMap{}</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/jit.html#save"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.save" title="Permalink to this definition">¶</a></dt>
<dd><p>Save an offline version of this module for use in a separate process. The saved
module serializes all of the methods, submodules, parameters, and attributes of this
module. It can be loaded into the C++ API using <code class="docutils literal notranslate"><span class="pre">torch::jit::load(filename)</span></code> or into the Python
API with <a class="reference internal" href="#torch.jit.load" title="torch.jit.load"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.load</span></code></a>.</p>
<p>To be able to save a module, it must not make any calls to native Python functions.
This means that all submodules must be subclasses of <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> as well.</p>
<div class="admonition danger">
<p class="admonition-title">Danger</p>
<p>All modules, no matter their device, are always loaded onto the CPU during loading.
This is different from <a class="reference internal" href="torch.html#torch.load" title="torch.load"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.load()</span></code></a>’s semantics and may change in the future.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>m</strong> – A <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> to save.</p></li>
<li><p><strong>f</strong> – A file-like object (has to implement write and flush) or a string
containing a file name.</p></li>
<li><p><strong>_extra_files</strong> – Map from filename to contents which will be stored as part of ‘f’.</p></li>
</ul>
</dd>
</dl>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>If you are using Python 2, <code class="docutils literal notranslate"><span class="pre">torch.jit.save</span></code> does NOT support <code class="xref any docutils literal notranslate"><span class="pre">StringIO.StringIO</span></code>
as a valid file-like object. This is because the write method should return
the number of bytes written; <code class="docutils literal notranslate"><span class="pre">StringIO.write()</span></code> does not do this.</p>
<p>Please use something like <code class="docutils literal notranslate"><span class="pre">io.BytesIO</span></code> instead.</p>
</div>
<p>Example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">io</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">10</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>

<span class="c1"># Save to file</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s1">&#39;scriptmodule.pt&#39;</span><span class="p">)</span>
<span class="c1"># This line is equivalent to the previous</span>
<span class="n">m</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s2">&quot;scriptmodule.pt&quot;</span><span class="p">)</span>

<span class="c1"># Save to io.BytesIO buffer</span>
<span class="n">buffer</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">BytesIO</span><span class="p">()</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">buffer</span><span class="p">)</span>

<span class="c1"># Save with extra files</span>
<span class="n">extra_files</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">ExtraFilesMap</span><span class="p">()</span>
<span class="n">extra_files</span><span class="p">[</span><span class="s1">&#39;foo.txt&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;bar&#39;</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s1">&#39;scriptmodule.pt&#39;</span><span class="p">,</span> <span class="n">_extra_files</span><span class="o">=</span><span class="n">extra_files</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="torch.jit.load">
<code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">load</code><span class="sig-paren">(</span><em class="sig-param">f</em>, <em class="sig-param">map_location=None</em>, <em class="sig-param">_extra_files=ExtraFilesMap{}</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/jit.html#load"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.load" title="Permalink to this definition">¶</a></dt>
<dd><p>Load a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> or <a class="reference internal" href="#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a> previously
saved with <a class="reference internal" href="#torch.jit.save" title="torch.jit.save"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.save</span></code></a></p>
<p>All previously saved modules, no matter their device, are first loaded onto CPU,
and then are moved to the devices they were saved from. If this fails (e.g. because
the run time system doesn’t have certain devices), an exception is raised.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>f</strong> – a file-like object (has to implement read, readline, tell, and seek),
or a string containing a file name</p></li>
<li><p><strong>map_location</strong> (<em>string</em><em> or </em><a class="reference internal" href="tensor_attributes.html#torch.torch.device" title="torch.torch.device"><em>torch.device</em></a>) – A simplified version of <code class="docutils literal notranslate"><span class="pre">map_location</span></code> in
<code class="docutils literal notranslate"><span class="pre">torch.save</span></code> used to dynamically remap storages to an alternative set of devices.</p></li>
<li><p><strong>_extra_files</strong> (<em>dictionary of filename to content</em>) – The extra
filenames given in the map would be loaded and their content
would be stored in the provided map.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> object.</p>
</dd>
</dl>
<p>Example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">io</span>

<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">&#39;scriptmodule.pt&#39;</span><span class="p">)</span>

<span class="c1"># Load ScriptModule from io.BytesIO object</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;scriptmodule.pt&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">buffer</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">BytesIO</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>

<span class="c1"># Load all tensors to the original device</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">buffer</span><span class="p">)</span>

<span class="c1"># Load all tensors onto CPU, using a device</span>
<span class="n">buffer</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">map_location</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">device</span><span class="p">(</span><span class="s1">&#39;cpu&#39;</span><span class="p">))</span>

<span class="c1"># Load all tensors onto CPU, using a string</span>
<span class="n">buffer</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">map_location</span><span class="o">=</span><span class="s1">&#39;cpu&#39;</span><span class="p">)</span>

<span class="c1"># Load with extra files.</span>
<span class="n">extra_files</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">ExtraFilesMap</span><span class="p">()</span>
<span class="n">extra_files</span><span class="p">[</span><span class="s1">&#39;foo.txt&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;bar&#39;</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">&#39;scriptmodule.pt&#39;</span><span class="p">,</span> <span class="n">_extra_files</span><span class="o">=</span><span class="n">extra_files</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">extra_files</span><span class="p">[</span><span class="s1">&#39;foo.txt&#39;</span><span class="p">])</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="torch.jit.ignore">
<code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">ignore</code><span class="sig-paren">(</span><em class="sig-param">drop=False</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/_jit_internal.html#ignore"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.ignore" title="Permalink to this definition">¶</a></dt>
<dd><p>This decorator indicates to the compiler that a function or method should
be ignored and left as a Python function. This allows you to leave code in
your model that is not yet TorchScript compatible. If called from TorchScript,
ignored functions will dispatch the call to the Python interpreter. Models with ignored
functions cannot be exported; use <a class="reference internal" href="#torch.jit.unused" title="torch.jit.unused"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.unused</span></code></a> instead.</p>
<p>Example (using <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code> on a method):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">ignore</span>
    <span class="k">def</span> <span class="nf">debugger</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">pdb</span>
        <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">+=</span> <span class="mi">10</span>
        <span class="c1"># The compiler would normally try to compile `debugger`,</span>
        <span class="c1"># but since it is `@ignore`d, it will be left as a call</span>
        <span class="c1"># to Python</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">debugger</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>

<span class="c1"># Error! The call `debugger` cannot be saved since it calls into Python</span>
<span class="n">m</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s2">&quot;m.pt&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Example (using <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore(drop=True)</span></code> on a method):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">ignore</span><span class="p">(</span><span class="n">drop</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">training_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">pdb</span>
        <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">training</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">training_method</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>

<span class="c1"># This is OK since `training_method` is not saved, the call is replaced</span>
<span class="c1"># with a `raise`.</span>
<span class="n">m</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s2">&quot;m.pt&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="torch.jit.unused">
<code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">unused</code><span class="sig-paren">(</span><em class="sig-param">fn</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/_jit_internal.html#unused"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.unused" title="Permalink to this definition">¶</a></dt>
<dd><p>This decorator indicates to the compiler that a function or method should
be ignored and replaced with the raising of an exception. This allows you
to leave code in your model that is not yet TorchScript compatible and still
export your model.</p>
<blockquote>
<div><p>Example (using <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.unused</span></code> on a method):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">use_memory_efficent</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_memory_efficent</span> <span class="o">=</span> <span class="n">use_memory_efficent</span>

    <span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">unused</span>
    <span class="k">def</span> <span class="nf">memory_efficient</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">pdb</span>
        <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">10</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="c1"># Use not-yet-scriptable memory efficient mode</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_memory_efficient</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">memory_efficient</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">10</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">(</span><span class="n">use_memory_efficent</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
<span class="n">m</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s2">&quot;m.pt&quot;</span><span class="p">)</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">(</span><span class="n">use_memory_efficient</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="c1"># exception raised</span>
<span class="n">m</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
</dd></dl>

</div>
<div class="section" id="mixing-tracing-and-scripting">
<h2><a class="toc-backref" href="#id5">Mixing Tracing and Scripting</a><a class="headerlink" href="#mixing-tracing-and-scripting" title="Permalink to this headline">¶</a></h2>
<p>In many cases either tracing or scripting is an easier approach for converting a model to TorchScript.
Tracing and scripting can be composed to suit the particular requirements
of a part of a model.</p>
<p>Scripted functions can call traced functions. This is particularly useful when you need
to use control-flow around a simple feed-forward model. For instance the beam search
of a sequence to sequence model will typically be written in script but can call an
encoder module generated using tracing.</p>
<p>Example (calling a traced function in script):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>

<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
    <span class="k">return</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>

<span class="n">traced_foo</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">foo</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">traced_foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<p>Traced functions can call script functions. This is useful when a small part of
a model requires some control-flow even though most of the model is just a feed-forward
network. Control-flow inside of a script function called by a traced function is
preserved correctly.</p>
<p>Example (calling a script function in a traced function):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">y</span><span class="o">.</span><span class="n">max</span><span class="p">():</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">x</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">y</span>
    <span class="k">return</span> <span class="n">r</span>


<span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="o">+</span> <span class="n">z</span>

<span class="n">traced_bar</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">bar</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</span>
</pre></div>
</div>
<p>This composition also works for <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>s as well, where it can be used to generate
a submodule using tracing that can be called from the methods of a script module.</p>
<p>Example (using a traced module):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torchvision</span>

<span class="k">class</span> <span class="nc">MyScriptModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyScriptModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">means</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="mf">103.939</span><span class="p">,</span> <span class="mf">116.779</span><span class="p">,</span> <span class="mf">123.68</span><span class="p">])</span>
                                        <span class="o">.</span><span class="n">resize_</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">resnet</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">torchvision</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">resnet18</span><span class="p">(),</span>
                                      <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">224</span><span class="p">,</span> <span class="mi">224</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">resnet</span><span class="p">(</span><span class="nb">input</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">means</span><span class="p">)</span>

<span class="n">my_script_module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyScriptModule</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="torchscript-language">
<h2><a class="toc-backref" href="#id6">TorchScript Language</a><a class="headerlink" href="#torchscript-language" title="Permalink to this headline">¶</a></h2>
<p>TorchScript is a statically typed subset of Python, so many Python features apply
directly to TorchScript. See the full <a class="reference internal" href="jit_language_reference.html#language-reference"><span class="std std-ref">TorchScript Language Reference</span></a> for details.</p>
</div>
<div class="section" id="built-in-functions-and-modules">
<span id="builtin-functions"></span><h2><a class="toc-backref" href="#id7">Built-in Functions and Modules</a><a class="headerlink" href="#built-in-functions-and-modules" title="Permalink to this headline">¶</a></h2>
<p>TorchScript supports the use of most PyTorch functions and many Python built-ins.
See <a class="reference internal" href="jit_builtin_functions.html#builtin-functions"><span class="std std-ref">TorchScript Builtins</span></a> for a full reference of supported functions.</p>
<div class="section" id="pytorch-functions-and-modules">
<h3><a class="toc-backref" href="#id8">PyTorch Functions and Modules</a><a class="headerlink" href="#pytorch-functions-and-modules" title="Permalink to this headline">¶</a></h3>
<p>TorchScript supports a subset of the tensor and neural network
functions that PyTorch provides. Most methods on Tensor as well as functions in
the <code class="docutils literal notranslate"><span class="pre">torch</span></code> namespace, all functions in <code class="docutils literal notranslate"><span class="pre">torch.nn.functional</span></code> and
most modules from <code class="docutils literal notranslate"><span class="pre">torch.nn</span></code> are supported in TorchScript.</p>
<p>See <a class="reference internal" href="jit_unsupported.html#jit-unsupported"><span class="std std-ref">TorchScript Unsupported Pytorch Constructs</span></a> for a list of unsupported PyTorch functions and modules.</p>
</div>
<div class="section" id="python-functions-and-modules">
<h3><a class="toc-backref" href="#id9">Python Functions and Modules</a><a class="headerlink" href="#python-functions-and-modules" title="Permalink to this headline">¶</a></h3>
<p>Many of Python’s <a class="reference external" href="https://docs.python.org/3/library/functions.html">built-in functions</a> are supported in TorchScript.
The <a class="reference external" href="https://docs.python.org/3/library/math.html#module-math" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">math</span></code></a> module is also supported (see <span class="xref std std-ref">math-module</span> for details), but no other Python modules
(built-in or third party) are supported.</p>
</div>
<div class="section" id="python-language-reference-comparison">
<h3><a class="toc-backref" href="#id10">Python Language Reference Comparison</a><a class="headerlink" href="#python-language-reference-comparison" title="Permalink to this headline">¶</a></h3>
<p>For a full listing of supported Python features, see <a class="reference internal" href="jit_python_reference.html#python-language-reference"><span class="std std-ref">Python Language Reference Coverage</span></a>.</p>
</div>
</div>
<div class="section" id="debugging">
<h2><a class="toc-backref" href="#id11">Debugging</a><a class="headerlink" href="#debugging" title="Permalink to this headline">¶</a></h2>
<div class="section" id="disable-jit-for-debugging">
<span id="disable-torchscript"></span><h3><a class="toc-backref" href="#id12">Disable JIT for Debugging</a><a class="headerlink" href="#disable-jit-for-debugging" title="Permalink to this headline">¶</a></h3>
<dl class="envvar">
<dt id="envvar-PYTORCH_JIT">
<code class="sig-name descname">PYTORCH_JIT</code><a class="headerlink" href="#envvar-PYTORCH_JIT" title="Permalink to this definition">¶</a></dt>
<dd><p>Setting the environment variable <code class="docutils literal notranslate"><span class="pre">PYTORCH_JIT=0</span></code> will disable all script
and tracing annotations. If there is hard-to-debug error in one of your
TorchScript model, you can use this flag to force everything to run using native
Python. Since TorchScript (scripting and tracing) are disabled with this flag,
you can use tools like <code class="docutils literal notranslate"><span class="pre">pdb</span></code> to debug the model code.</p>
<p>Given an example</p>
<blockquote>
<div><p>&#64;torch.jit.script
def scripted_fn(x : torch.Tensor):</p>
<blockquote>
<div><dl class="simple">
<dt>for i in range(12):</dt><dd><p>x = x + x</p>
</dd>
</dl>
<p>return x</p>
</div></blockquote>
<dl class="simple">
<dt>def fn(x):</dt><dd><p>x = torch.neg(x)
import pdb; pdb.set_trace()
return scripted_fn(x)</p>
</dd>
</dl>
<p>traced_fn = torch.jit.trace(fn, (torch.rand(4, 5),))
traced_fn(torch.rand(3, 4))</p>
</div></blockquote>
<p>Debugging this script with <code class="docutils literal notranslate"><span class="pre">pdb</span></code> works except for when we invoke the <a class="reference internal" href="#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.script</span></code></a>
function. We can globally disable JIT, so that we can call the <a class="reference internal" href="#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.script</span></code></a>
function as a normal Python function and not compile it. If the above script
is called <code class="docutils literal notranslate"><span class="pre">disable_jit_example.py</span></code>, we can invoke it like so:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ PYTORCH_JIT=0 python disable_jit_example.py
</pre></div>
</div>
<p>and we will be able to step into the <a class="reference internal" href="#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.script</span></code></a> function as a normal Python
function. To disable the TorchScript compiler for a specific function, see
<a class="reference internal" href="#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code></a>.</p>
</dd></dl>

</div>
<div class="section" id="inspecting-code">
<h3><a class="toc-backref" href="#id13">Inspecting Code</a><a class="headerlink" href="#inspecting-code" title="Permalink to this headline">¶</a></h3>
<p>TorchScript provides a code pretty-printer for all <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> instances. This
pretty-printer gives an interpretation of the script method’s code as valid
Python syntax. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="nb">len</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; torch.Tensor</span>
    <span class="n">rv</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">:</span>
            <span class="n">rv</span> <span class="o">=</span> <span class="n">rv</span> <span class="o">-</span> <span class="mf">1.0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">rv</span> <span class="o">=</span> <span class="n">rv</span> <span class="o">+</span> <span class="mf">1.0</span>
    <span class="k">return</span> <span class="n">rv</span>

<span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>
</pre></div>
</div>
<p>A <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> with a single <code class="docutils literal notranslate"><span class="pre">forward</span></code> method will have an attribute
<code class="docutils literal notranslate"><span class="pre">code</span></code>, which you can use to inspect the <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>’s code.
If the <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> has more than one method, you will need to access
<code class="docutils literal notranslate"><span class="pre">.code</span></code> on the method itself and not the module. We can inspect the
code of a method named <code class="docutils literal notranslate"><span class="pre">foo</span></code> on a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> by accessing <code class="docutils literal notranslate"><span class="pre">.foo.code</span></code>.
The example above produces this output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="nb">len</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tensor</span><span class="p">:</span>
    <span class="n">rv</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">pin_memory</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
    <span class="n">rv0</span> <span class="o">=</span> <span class="n">rv</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">torch</span><span class="o">.</span><span class="n">lt</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">10</span><span class="p">):</span>
            <span class="n">rv1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">rv0</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">rv1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">rv0</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">rv0</span> <span class="o">=</span> <span class="n">rv1</span>
    <span class="k">return</span> <span class="n">rv0</span>
</pre></div>
</div>
<p>This is TorchScript’s compilation of the code for the <code class="docutils literal notranslate"><span class="pre">forward</span></code> method.
You can use this to ensure TorchScript (tracing or scripting) has captured
your model code correctly.</p>
</div>
<div class="section" id="interpreting-graphs">
<h3><a class="toc-backref" href="#id14">Interpreting Graphs</a><a class="headerlink" href="#interpreting-graphs" title="Permalink to this headline">¶</a></h3>
<p>TorchScript also has a representation at a lower level than the code pretty-
printer, in the form of IR graphs.</p>
<p>TorchScript uses a static single assignment (SSA) intermediate representation
(IR) to represent computation. The instructions in this format consist of
ATen (the C++ backend of PyTorch) operators and other primitive operators,
including control flow operators for loops and conditionals. As an example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="nb">len</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; torch.Tensor</span>
    <span class="n">rv</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">:</span>
            <span class="n">rv</span> <span class="o">=</span> <span class="n">rv</span> <span class="o">-</span> <span class="mf">1.0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">rv</span> <span class="o">=</span> <span class="n">rv</span> <span class="o">+</span> <span class="mf">1.0</span>
    <span class="k">return</span> <span class="n">rv</span>

<span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">graph</span></code> follows the same rules described in the <a class="reference internal" href="#inspecting-code">Inspecting Code</a> section
with regard to <code class="docutils literal notranslate"><span class="pre">forward</span></code> method lookup.</p>
<p>The example script above produces the graph:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>graph(%len.1 : int):
  %24 : int = prim::Constant[value=1]()
  %17 : bool = prim::Constant[value=1]() # test.py:10:5
  %12 : bool? = prim::Constant()
  %10 : Device? = prim::Constant()
  %6 : int? = prim::Constant()
  %1 : int = prim::Constant[value=3]() # test.py:9:22
  %2 : int = prim::Constant[value=4]() # test.py:9:25
  %20 : int = prim::Constant[value=10]() # test.py:11:16
  %23 : float = prim::Constant[value=1]() # test.py:12:23
  %4 : int[] = prim::ListConstruct(%1, %2)
  %rv.1 : Tensor = aten::zeros(%4, %6, %6, %10, %12) # test.py:9:10
  %rv : Tensor = prim::Loop(%len.1, %17, %rv.1) # test.py:10:5
    block0(%i.1 : int, %rv.14 : Tensor):
      %21 : bool = aten::lt(%i.1, %20) # test.py:11:12
      %rv.13 : Tensor = prim::If(%21) # test.py:11:9
        block0():
          %rv.3 : Tensor = aten::sub(%rv.14, %23, %24) # test.py:12:18
          -&gt; (%rv.3)
        block1():
          %rv.6 : Tensor = aten::add(%rv.14, %23, %24) # test.py:14:18
          -&gt; (%rv.6)
      -&gt; (%17, %rv.13)
  return (%rv)
</pre></div>
</div>
<p>Take the instruction <code class="docutils literal notranslate"><span class="pre">%rv.1</span> <span class="pre">:</span> <span class="pre">Tensor</span> <span class="pre">=</span> <span class="pre">aten::zeros(%4,</span> <span class="pre">%6,</span> <span class="pre">%6,</span> <span class="pre">%10,</span> <span class="pre">%12)</span> <span class="pre">#</span> <span class="pre">test.py:9:10</span></code> for
example.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">%rv.1</span> <span class="pre">:</span> <span class="pre">Tensor</span></code> means we assign the output to a (unique) value named <code class="docutils literal notranslate"><span class="pre">rv.1</span></code>, that value is of <code class="docutils literal notranslate"><span class="pre">Tensor</span></code> type and that we do not know its concrete shape.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">aten::zeros</span></code> is the operator (equivalent to <code class="docutils literal notranslate"><span class="pre">torch.zeros</span></code>) and the input list <code class="docutils literal notranslate"><span class="pre">(%4,</span> <span class="pre">%6,</span> <span class="pre">%6,</span> <span class="pre">%10,</span> <span class="pre">%12)</span></code> specifies which values in scope should be passed as inputs. The schema for built-in functions like <code class="docutils literal notranslate"><span class="pre">aten::zeros</span></code> can be found at <a href="#id20"><span class="problematic" id="id21">`Builtin Functions`_</span></a>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">#</span> <span class="pre">test.py:9:10</span></code> is the location in the original source file that generated this instruction. In this case, it is a file named <cite>test.py</cite>, on line 9, and at character 10.</p></li>
</ul>
<p>Notice that operators can also have associated <code class="docutils literal notranslate"><span class="pre">blocks</span></code>, namely the
<code class="docutils literal notranslate"><span class="pre">prim::Loop</span></code> and <code class="docutils literal notranslate"><span class="pre">prim::If</span></code> operators. In the graph print-out, these
operators are formatted to reflect their equivalent source code forms
to facilitate easy debugging.</p>
<p>Graphs can be inspected as shown to confirm that the computation described
by a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> is correct, in both automated and manual fashion, as
described below.</p>
</div>
<div class="section" id="tracer">
<h3><a class="toc-backref" href="#id15">Tracer</a><a class="headerlink" href="#tracer" title="Permalink to this headline">¶</a></h3>
<div class="section" id="tracing-edge-cases">
<h4>Tracing Edge Cases<a class="headerlink" href="#tracing-edge-cases" title="Permalink to this headline">¶</a></h4>
<p>There are some edge cases that exist where the trace of a given Python
function/module will not be representative of the underlying code. These
cases can include:</p>
<ul class="simple">
<li><p>Tracing of control flow that is dependent on inputs (e.g. tensor shapes)</p></li>
<li><p>Tracing of in-place operations of tensor views (e.g. indexing on the left-hand side of an assignment)</p></li>
</ul>
<p>Note that these cases may in fact be traceable in the future.</p>
</div>
<div class="section" id="automatic-trace-checking">
<h4>Automatic Trace Checking<a class="headerlink" href="#automatic-trace-checking" title="Permalink to this headline">¶</a></h4>
<p>One way to automatically catch many errors in traces is by using <code class="docutils literal notranslate"><span class="pre">check_inputs</span></code>
on the <code class="docutils literal notranslate"><span class="pre">torch.jit.trace()</span></code> API. <code class="docutils literal notranslate"><span class="pre">check_inputs</span></code> takes a list of tuples
of inputs that will be used to re-trace the computation and verify the
results. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">loop_in_traced_fn</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">)):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">result</span> <span class="o">*</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">result</span>

<span class="n">inputs</span> <span class="o">=</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">),)</span>
<span class="n">check_inputs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">),),</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">),)]</span>

<span class="n">traced</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">loop_in_traced_fn</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">check_inputs</span><span class="o">=</span><span class="n">check_inputs</span><span class="p">)</span>
</pre></div>
</div>
<p>Gives us the following diagnostic information:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>ERROR: Graphs differed across invocations!
Graph diff:

            graph(%x : Tensor) {
            %1 : int = prim::Constant[value=0]()
            %2 : int = prim::Constant[value=0]()
            %result.1 : Tensor = aten::select(%x, %1, %2)
            %4 : int = prim::Constant[value=0]()
            %5 : int = prim::Constant[value=0]()
            %6 : Tensor = aten::select(%x, %4, %5)
            %result.2 : Tensor = aten::mul(%result.1, %6)
            %8 : int = prim::Constant[value=0]()
            %9 : int = prim::Constant[value=1]()
            %10 : Tensor = aten::select(%x, %8, %9)
        -   %result : Tensor = aten::mul(%result.2, %10)
        +   %result.3 : Tensor = aten::mul(%result.2, %10)
        ?          ++
            %12 : int = prim::Constant[value=0]()
            %13 : int = prim::Constant[value=2]()
            %14 : Tensor = aten::select(%x, %12, %13)
        +   %result : Tensor = aten::mul(%result.3, %14)
        +   %16 : int = prim::Constant[value=0]()
        +   %17 : int = prim::Constant[value=3]()
        +   %18 : Tensor = aten::select(%x, %16, %17)
        -   %15 : Tensor = aten::mul(%result, %14)
        ?     ^                                 ^
        +   %19 : Tensor = aten::mul(%result, %18)
        ?     ^                                 ^
        -   return (%15);
        ?             ^
        +   return (%19);
        ?             ^
            }
</pre></div>
</div>
<p>This message indicates to us that the computation differed between when
we first traced it and when we traced it with the <code class="docutils literal notranslate"><span class="pre">check_inputs</span></code>. Indeed,
the loop within the body of <code class="docutils literal notranslate"><span class="pre">loop_in_traced_fn</span></code> depends on the shape
of the input <code class="docutils literal notranslate"><span class="pre">x</span></code>, and thus when we try another <code class="docutils literal notranslate"><span class="pre">x</span></code> with a different
shape, the trace differs.</p>
<p>In this case, data-dependent control flow like this can be captured using
<a class="reference internal" href="#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.script()</span></code></a> instead:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fn</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">)):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">result</span> <span class="o">*</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">result</span>

<span class="n">inputs</span> <span class="o">=</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">),)</span>
<span class="n">check_inputs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">),),</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">),)]</span>

<span class="n">scripted_fn</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">scripted_fn</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
<span class="c1">#print(str(scripted_fn.graph).strip())</span>

<span class="k">for</span> <span class="n">input_tuple</span> <span class="ow">in</span> <span class="p">[</span><span class="n">inputs</span><span class="p">]</span> <span class="o">+</span> <span class="n">check_inputs</span><span class="p">:</span>
    <span class="n">torch</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">assert_allclose</span><span class="p">(</span><span class="n">fn</span><span class="p">(</span><span class="o">*</span><span class="n">input_tuple</span><span class="p">),</span> <span class="n">scripted_fn</span><span class="p">(</span><span class="o">*</span><span class="n">input_tuple</span><span class="p">))</span>
</pre></div>
</div>
<p>Which produces:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">graph</span><span class="p">(</span><span class="o">%</span><span class="n">x</span> <span class="p">:</span> <span class="n">Tensor</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">%</span><span class="mi">5</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Constant</span><span class="p">[</span><span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">]()</span>
    <span class="o">%</span><span class="mi">1</span> <span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Constant</span><span class="p">[</span><span class="n">value</span><span class="o">=</span><span class="mi">0</span><span class="p">]()</span>
    <span class="o">%</span><span class="n">result</span><span class="o">.</span><span class="mi">1</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">select</span><span class="p">(</span><span class="o">%</span><span class="n">x</span><span class="p">,</span> <span class="o">%</span><span class="mi">1</span><span class="p">,</span> <span class="o">%</span><span class="mi">1</span><span class="p">)</span>
    <span class="o">%</span><span class="mi">4</span> <span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">size</span><span class="p">(</span><span class="o">%</span><span class="n">x</span><span class="p">,</span> <span class="o">%</span><span class="mi">1</span><span class="p">)</span>
    <span class="o">%</span><span class="n">result</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Loop</span><span class="p">(</span><span class="o">%</span><span class="mi">4</span><span class="p">,</span> <span class="o">%</span><span class="mi">5</span><span class="p">,</span> <span class="o">%</span><span class="n">result</span><span class="o">.</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">block0</span><span class="p">(</span><span class="o">%</span><span class="n">i</span> <span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="o">%</span><span class="mi">7</span> <span class="p">:</span> <span class="n">Tensor</span><span class="p">)</span> <span class="p">{</span>
        <span class="o">%</span><span class="mi">10</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">select</span><span class="p">(</span><span class="o">%</span><span class="n">x</span><span class="p">,</span> <span class="o">%</span><span class="mi">1</span><span class="p">,</span> <span class="o">%</span><span class="n">i</span><span class="p">)</span>
        <span class="o">%</span><span class="n">result</span><span class="o">.</span><span class="mi">2</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">mul</span><span class="p">(</span><span class="o">%</span><span class="mi">7</span><span class="p">,</span> <span class="o">%</span><span class="mi">10</span><span class="p">)</span>
        <span class="o">-&gt;</span> <span class="p">(</span><span class="o">%</span><span class="mi">5</span><span class="p">,</span> <span class="o">%</span><span class="n">result</span><span class="o">.</span><span class="mi">2</span><span class="p">)</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="p">(</span><span class="o">%</span><span class="n">result</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="tracer-warnings">
<h4>Tracer Warnings<a class="headerlink" href="#tracer-warnings" title="Permalink to this headline">¶</a></h4>
<p>The tracer produces warnings for several problematic patterns in traced
computation. As an example, take a trace of a function that contains an
in-place assignment on a slice (a view) of a Tensor:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fill_row_zero</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="o">*</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">x</span>

<span class="n">traced</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">fill_row_zero</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">),))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">traced</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
</pre></div>
</div>
<p>Produces several warnings and a graph which simply returns the input:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>fill_row_zero.py:4: TracerWarning: There are 2 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
    x[0] = torch.rand(*x.shape[1:2])
fill_row_zero.py:6: TracerWarning: Output nr 1. of the traced function does not match the corresponding output of the Python function. Detailed error:
Not within tolerance rtol=1e-05 atol=1e-05 at input[0, 1] (0.09115803241729736 vs. 0.6782537698745728) and 3 other locations (33.00%)
    traced = torch.jit.trace(fill_row_zero, (torch.rand(3, 4),))
graph(%0 : Float(3, 4)) {
    return (%0);
}
</pre></div>
</div>
<p>We can fix this by modifying the code to not use the in-place update, but
rather build up the result tensor out-of-place with <code class="docutils literal notranslate"><span class="pre">torch.cat</span></code>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fill_row_zero</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">]),</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">]),</span> <span class="n">dim</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">x</span>

<span class="n">traced</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">fill_row_zero</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">),))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">traced</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="id1">
<span id="id2"></span><h3><a class="toc-backref" href="#id16">Built-in Functions and Modules</a><a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<p>See <a class="reference internal" href="jit_builtin_functions.html#builtin-functions"><span class="std std-ref">TorchScript Builtins</span></a> for a full reference of supported functions.</p>
</div>
</div>
<div class="section" id="frequently-asked-questions">
<h2><a class="toc-backref" href="#id17">Frequently Asked Questions</a><a class="headerlink" href="#frequently-asked-questions" title="Permalink to this headline">¶</a></h2>
<p>Q: I would like to train a model on GPU and do inference on CPU. What are the
best practices?</p>
<blockquote>
<div><p>First convert your model from GPU to CPU and then save it, like so:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">cpu_model</span> <span class="o">=</span> <span class="n">gpu_model</span><span class="o">.</span><span class="n">cpu</span><span class="p">()</span>
<span class="n">sample_input_cpu</span> <span class="o">=</span> <span class="n">sample_input_gpu</span><span class="o">.</span><span class="n">cpu</span><span class="p">()</span>
<span class="n">traced_cpu</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">traced_cpu</span><span class="p">,</span> <span class="n">sample_input_cpu</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">traced_cpu</span><span class="p">,</span> <span class="s2">&quot;cpu.pth&quot;</span><span class="p">)</span>

<span class="n">traced_gpu</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">traced_gpu</span><span class="p">,</span> <span class="n">sample_input_gpu</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">traced_gpu</span><span class="p">,</span> <span class="s2">&quot;gpu.pth&quot;</span><span class="p">)</span>

<span class="c1"># ... later, when using the model:</span>

<span class="k">if</span> <span class="n">use_gpu</span><span class="p">:</span>
  <span class="n">model</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s2">&quot;gpu.pth&quot;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
  <span class="n">model</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s2">&quot;cpu.pth&quot;</span><span class="p">)</span>

<span class="n">model</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
</pre></div>
</div>
<p>This is recommended because the tracer may witness tensor creation on a
specific device, so casting an already-loaded model may have unexpected
effects. Casting the model <em>before</em> saving it ensures that the tracer has
the correct device information.</p>
</div></blockquote>
<p>Q: How do I store attributes on a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>?</p>
<blockquote>
<div><p>Say we have a model like:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Model</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Model</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">Model</span><span class="p">())</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">Model</span></code> is instantiated it will result in a compilation error
since the compiler doesn’t know about <code class="docutils literal notranslate"><span class="pre">x</span></code>. There are 4 ways to inform the
compiler of attributes on <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>:</p>
<p>1. <code class="docutils literal notranslate"><span class="pre">nn.Parameter</span></code> - Values wrapped in <code class="docutils literal notranslate"><span class="pre">nn.Parameter</span></code> will work as they
do on <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>s</p>
<p>2. <code class="docutils literal notranslate"><span class="pre">register_buffer</span></code> - Values wrapped in <code class="docutils literal notranslate"><span class="pre">register_buffer</span></code> will work as
they do on <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>s. This is equivalent to an attribute (see 4) of type
<code class="docutils literal notranslate"><span class="pre">Tensor</span></code>.</p>
<p>3. Constants - Annotating a class member as <code class="docutils literal notranslate"><span class="pre">Final</span></code> (or adding it to a list called
<code class="docutils literal notranslate"><span class="pre">__constants__</span></code> at the class definition level) will mark the contained names
as constants. Constants are saved directly in the code of the model. See
<a href="#id22"><span class="problematic" id="id23">`Python-defined Constants`_</span></a> for details.</p>
<p>4. Attributes - Values that are a <a href="#id24"><span class="problematic" id="id25">`supported type`_</span></a> can be added as mutable
attributes. Most types can be inferred but some may need to be specified, see
<a href="#id26"><span class="problematic" id="id27">`Module Attributes`_</span></a> for details.</p>
</div></blockquote>
<p>Q: I would like to trace module’s method but I keep getting this error:</p>
<p><code class="docutils literal notranslate"><span class="pre">RuntimeError:</span> <span class="pre">Cannot</span> <span class="pre">insert</span> <span class="pre">a</span> <span class="pre">Tensor</span> <span class="pre">that</span> <span class="pre">requires</span> <span class="pre">grad</span> <span class="pre">as</span> <span class="pre">a</span> <span class="pre">constant.</span> <span class="pre">Consider</span> <span class="pre">making</span> <span class="pre">it</span> <span class="pre">a</span> <span class="pre">parameter</span> <span class="pre">or</span> <span class="pre">input,</span> <span class="pre">or</span> <span class="pre">detaching</span> <span class="pre">the</span> <span class="pre">gradient</span></code></p>
<blockquote>
<div><p>This error usually means that the method you are tracing uses a module’s parameters and
you are passing the module’s method instead of the module instance (e.g. <code class="docutils literal notranslate"><span class="pre">my_module_instance.forward</span></code> vs <code class="docutils literal notranslate"><span class="pre">my_module_instance</span></code>).</p>
<blockquote>
<div><ul class="simple">
<li><p>Invoking <code class="docutils literal notranslate"><span class="pre">trace</span></code> with a module’s method captures module parameters (which may require gradients) as <strong>constants</strong>.</p></li>
<li><p>On the other hand, invoking <code class="docutils literal notranslate"><span class="pre">trace</span></code> with module’s instance (e.g. <code class="docutils literal notranslate"><span class="pre">my_module</span></code>) creates a new module and correctly copies parameters into the new module, so they can accumulate gradients if required.</p></li>
</ul>
</div></blockquote>
<p>To trace a specific method on a module, see <a class="reference internal" href="#torch.jit.trace_module" title="torch.jit.trace_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.trace_module</span></code></a></p>
</div></blockquote>
</div>
<div class="section" id="appendix">
<h2><a class="toc-backref" href="#id18">Appendix</a><a class="headerlink" href="#appendix" title="Permalink to this headline">¶</a></h2>
<div class="section" id="migrating-to-pytorch-1-2-recursive-scripting-api">
<h3><a class="toc-backref" href="#id19">Migrating to PyTorch 1.2 Recursive Scripting API</a><a class="headerlink" href="#migrating-to-pytorch-1-2-recursive-scripting-api" title="Permalink to this headline">¶</a></h3>
<p>This section details the changes to TorchScript in PyTorch 1.2. If you are new to TorchScript you can
skip this section. There are two main changes to the TorchScript API with PyTorch 1.2.</p>
<p>1. <a class="reference internal" href="#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.script</span></code></a> will now attempt to recursively compile functions,
methods, and classes that it encounters. Once you call <code class="docutils literal notranslate"><span class="pre">torch.jit.script</span></code>,
compilation is “opt-out”, rather than “opt-in”.</p>
<p>2. <code class="docutils literal notranslate"><span class="pre">torch.jit.script(nn_module_instance)</span></code> is now the preferred way to create
<a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>s, instead of inheriting from <code class="docutils literal notranslate"><span class="pre">torch.jit.ScriptModule</span></code>.
These changes combine to provide a simpler, easier-to-use API for converting
your <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>s into <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>s, ready to be optimized and executed in a
non-Python environment.</p>
<p>The new usage looks like this:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="kn">import</span> <span class="nn">torch.nn.functional</span> <span class="k">as</span> <span class="nn">F</span>

<span class="k">class</span> <span class="nc">Model</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Model</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv1</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv2</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">conv1</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">F</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">conv2</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>

<span class="n">my_model</span> <span class="o">=</span> <span class="n">Model</span><span class="p">()</span>
<span class="n">my_scripted_model</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">my_model</span><span class="p">)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>The module’s <code class="docutils literal notranslate"><span class="pre">forward</span></code> is compiled by default. Methods called from <code class="docutils literal notranslate"><span class="pre">forward</span></code> are lazily compiled in the order they are used in <code class="docutils literal notranslate"><span class="pre">forward</span></code>.</p></li>
<li><p>To compile a method other than <code class="docutils literal notranslate"><span class="pre">forward</span></code> that is not called from <code class="docutils literal notranslate"><span class="pre">forward</span></code>, add <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.export</span></code>.</p></li>
<li><p>To stop the compiler from compiling a method, add <a class="reference internal" href="#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code></a> or <a class="reference internal" href="#torch.jit.unused" title="torch.jit.unused"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.unused</span></code></a>. <code class="docutils literal notranslate"><span class="pre">&#64;ignore</span></code> leaves the</p></li>
<li><p>method as a call to python, and <code class="docutils literal notranslate"><span class="pre">&#64;unused</span></code> replaces it with an exception. <code class="docutils literal notranslate"><span class="pre">&#64;ignored</span></code> cannot be exported; <code class="docutils literal notranslate"><span class="pre">&#64;unused</span></code> can.</p></li>
<li><p>Most attribute types can be inferred, so <code class="docutils literal notranslate"><span class="pre">torch.jit.Attribute</span></code> is not necessary. For empty container types, annotate their types using <a class="reference external" href="https://www.python.org/dev/peps/pep-0526/#class-and-instance-variable-annotations">PEP 526-style</a> class annotations.</p></li>
<li><p>Constants can be marked with a <code class="docutils literal notranslate"><span class="pre">Final</span></code> class annotation instead of adding the name of the member to <code class="docutils literal notranslate"><span class="pre">__constants__</span></code>.</p></li>
<li><p>Python 3 type hints can be used in place of <code class="docutils literal notranslate"><span class="pre">torch.jit.annotate</span></code></p></li>
</ul>
<dl class="simple">
<dt>As a result of these changes, the following items are considered deprecated and should not appear in new code:</dt><dd><ul class="simple">
<li><p>The <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.script_method</span></code> decorator</p></li>
<li><p>Classes that inherit from <code class="docutils literal notranslate"><span class="pre">torch.jit.ScriptModule</span></code></p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">torch.jit.Attribute</span></code> wrapper class</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">__constants__</span></code> array</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">torch.jit.annotate</span></code> function</p></li>
</ul>
</dd>
</dl>
<div class="section" id="modules">
<h4>Modules<a class="headerlink" href="#modules" title="Permalink to this headline">¶</a></h4>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The <a class="reference internal" href="#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code></a> annotation’s behavior changes in
PyTorch 1.2. Before PyTorch 1.2 the &#64;ignore decorator was used to make a function
or method callable from code that is exported. To get this functionality back,
use <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.unused()</span></code>. <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code> is now equivalent
to <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore(drop=False)</span></code>. See <a class="reference internal" href="#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code></a>
and <a class="reference internal" href="#torch.jit.unused" title="torch.jit.unused"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.unused</span></code></a> for details.</p>
</div>
<p>When passed to the <a class="reference internal" href="#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.script</span></code></a> function, a <code class="docutils literal notranslate"><span class="pre">torch.nn.Module</span></code>’s data is
copied to a <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> and the TorchScript compiler compiles the module.
The module’s <code class="docutils literal notranslate"><span class="pre">forward</span></code> is compiled by default. Methods called from <code class="docutils literal notranslate"><span class="pre">forward</span></code> are
lazily compiled in the order they are used in <code class="docutils literal notranslate"><span class="pre">forward</span></code>, as well as any
<code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.export</span></code> methods.</p>
<dl class="function">
<dt id="torch.jit.export">
<code class="sig-prename descclassname">torch.jit.</code><code class="sig-name descname">export</code><span class="sig-paren">(</span><em class="sig-param">fn</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/_jit_internal.html#export"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.jit.export" title="Permalink to this definition">¶</a></dt>
<dd><p>This decorator indicates that a method on an <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code> is used as an entry point into a
<a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> and should be compiled.</p>
<p><code class="docutils literal notranslate"><span class="pre">forward</span></code> implicitly is assumed to be an entry point, so it does not need this decorator.
Functions and methods called from <code class="docutils literal notranslate"><span class="pre">forward</span></code> are compiled as they are seen
by the compiler, so they do not need this decorator either.</p>
<p>Example (using <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.export</span></code> on a method):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">implicitly_compiled_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">99</span>

    <span class="c1"># `forward` is implicitly decorated with `@torch.jit.export`,</span>
    <span class="c1"># so adding it here would have no effect</span>
    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">10</span>

    <span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">export</span>
    <span class="k">def</span> <span class="nf">another_forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="c1"># When the compiler sees this call, it will compile</span>
        <span class="c1"># `implicitly_compiled_method`</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">implicitly_compiled_method</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">unused_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">20</span>

<span class="c1"># `m` will contain compiled methods:</span>
<span class="c1">#     `forward`</span>
<span class="c1">#     `another_forward`</span>
<span class="c1">#     `implicitly_compiled_method`</span>
<span class="c1"># `unused_method` will not be compiled since it was not called from</span>
<span class="c1"># any compiled methods and wasn&#39;t decorated with `@torch.jit.export`</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="functions">
<h4>Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h4>
<p>Functions don’t change much, they can be decorated with <a class="reference internal" href="#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code></a> or <a class="reference internal" href="#torch.jit.unused" title="torch.jit.unused"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.unused</span></code></a> if needed.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Same behavior as pre-PyTorch 1.2</span>
<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">some_fn</span><span class="p">():</span>
    <span class="k">return</span> <span class="mi">2</span>

<span class="c1"># Marks a function as ignored, if nothing</span>
<span class="c1"># ever calls it then this has no effect</span>
<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">ignore</span>
<span class="k">def</span> <span class="nf">some_fn2</span><span class="p">():</span>
    <span class="k">return</span> <span class="mi">2</span>

<span class="c1"># As with ignore, if nothing calls it then it has no effect.</span>
<span class="c1"># If it is called in script it is replaced with an exception.</span>
<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">unused</span>
<span class="k">def</span> <span class="nf">some_fn3</span><span class="p">():</span>
  <span class="kn">import</span> <span class="nn">pdb</span><span class="p">;</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
  <span class="k">return</span> <span class="mi">4</span>

<span class="c1"># Doesn&#39;t do anything, this function is already</span>
<span class="c1"># the main entry point</span>
<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">export</span>
<span class="k">def</span> <span class="nf">some_fn4</span><span class="p">():</span>
    <span class="k">return</span> <span class="mi">2</span>
</pre></div>
</div>
</div>
<div class="section" id="torchscript-classes">
<h4>TorchScript Classes<a class="headerlink" href="#torchscript-classes" title="Permalink to this headline">¶</a></h4>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>TorchScript class support is experimental. Currently it is best suited
for simple record-like types (think a <code class="docutils literal notranslate"><span class="pre">NamedTuple</span></code> with methods
attached).</p>
</div>
<p>Everything in a user defined <a href="#id28"><span class="problematic" id="id29">`TorchScript Class`_</span></a> is exported by default, functions
can be decorated with <a class="reference internal" href="#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.ignore</span></code></a> if needed.</p>
</div>
<div class="section" id="attributes">
<h4>Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h4>
<p>The TorchScript compiler needs to know the types of <a href="#id30"><span class="problematic" id="id31">`module attributes`_</span></a>. Most types
can be inferred from the value of the member. Empty lists and dicts cannot have their
types inferred and must have their types annotated with <a class="reference external" href="https://www.python.org/dev/peps/pep-0526/#class-and-instance-variable-annotations">PEP 526-style</a> class annotations.
If a type cannot be inferred and is not explicitly annotated, it will not be added as an attribute
to the resulting <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a></p>
<p>Old API:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>
<span class="kn">import</span> <span class="nn">torch</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">ScriptModule</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">my_dict</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">Attribute</span><span class="p">({},</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">my_int</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">Attribute</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">MyModule</span><span class="p">()</span>
</pre></div>
</div>
<p>New API:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="n">my_dict</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="c1"># This type cannot be inferred and must be specified</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">my_dict</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># The attribute type here is inferred to be `int`</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">my_int</span> <span class="o">=</span> <span class="mi">20</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="constants">
<h4>Constants<a class="headerlink" href="#constants" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">Final</span></code> type constructor can be used to mark members as <a href="#id32"><span class="problematic" id="id33">`constant`_</span></a>. If members are not marked constant, they will be copied to the resulting <a class="reference internal" href="#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> as an attribute. Using <code class="docutils literal notranslate"><span class="pre">Final</span></code> opens opportunities for optimization if the value is known to be fixed and gives additional type safety.</p>
<p>Old API:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">ScriptModule</span><span class="p">):</span>
    <span class="n">__constants__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;my_constant&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">my_constant</span> <span class="o">=</span> <span class="mi">2</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">MyModule</span><span class="p">()</span>
</pre></div>
</div>
<p>New API:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing_extensions</span> <span class="kn">import</span> <span class="n">Final</span>
<span class="k">except</span><span class="p">:</span>
    <span class="c1"># If you don&#39;t have `typing_extensions` installed, you can use a</span>
    <span class="c1"># polyfill from `torch.jit`.</span>
    <span class="kn">from</span> <span class="nn">torch.jit</span> <span class="kn">import</span> <span class="n">Final</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>

    <span class="n">my_constant</span><span class="p">:</span> <span class="n">Final</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">my_constant</span> <span class="o">=</span> <span class="mi">2</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="variables">
<span id="python-3-type-hints"></span><h4>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h4>
<p>Containers are assumed to have type <code class="docutils literal notranslate"><span class="pre">Tensor</span></code> and be non-optional (see
<a href="#id34"><span class="problematic" id="id35">`Default Types`_</span></a> for more information). Previously, <code class="docutils literal notranslate"><span class="pre">torch.jit.annotate</span></code> was used to
tell the TorchScript compiler what the type should be. Python 3 style type hints are
now supported.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">Optional</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">make_dict</span><span class="p">(</span><span class="n">flag</span><span class="p">:</span> <span class="nb">bool</span><span class="p">):</span>
    <span class="n">x</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">x</span><span class="p">[</span><span class="s1">&#39;hi&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="n">b</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">if</span> <span class="n">flag</span><span class="p">:</span>
        <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="k">return</span> <span class="n">x</span><span class="p">,</span> <span class="n">b</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>


             </article>
             
            </div>
            <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="jit_builtin_functions.html" class="btn btn-neutral float-right" title="TorchScript Builtins" accesskey="n" rel="next">Next <img src="_static/images/chevron-right-orange.svg" class="next-page"></a>
      
      
        <a href="hub.html" class="btn btn-neutral" title="torch.hub" accesskey="p" rel="prev"><img src="_static/images/chevron-right-orange.svg" class="previous-page"> Previous</a>
      
    </div>
  

  

    <hr>

  

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Torch Contributors.

    </p>
  </div>
    
      <div>
        Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
      </div>
     

</footer>

          </div>
        </div>

        <div class="pytorch-content-right" id="pytorch-content-right">
          <div class="pytorch-right-menu" id="pytorch-right-menu">
            <div class="pytorch-side-scroll" id="pytorch-side-scroll-right">
              <ul>
<li><a class="reference internal" href="#">TorchScript</a><ul>
<li><a class="reference internal" href="#creating-torchscript-code">Creating TorchScript Code</a></li>
<li><a class="reference internal" href="#mixing-tracing-and-scripting">Mixing Tracing and Scripting</a></li>
<li><a class="reference internal" href="#torchscript-language">TorchScript Language</a></li>
<li><a class="reference internal" href="#built-in-functions-and-modules">Built-in Functions and Modules</a><ul>
<li><a class="reference internal" href="#pytorch-functions-and-modules">PyTorch Functions and Modules</a></li>
<li><a class="reference internal" href="#python-functions-and-modules">Python Functions and Modules</a></li>
<li><a class="reference internal" href="#python-language-reference-comparison">Python Language Reference Comparison</a></li>
</ul>
</li>
<li><a class="reference internal" href="#debugging">Debugging</a><ul>
<li><a class="reference internal" href="#disable-jit-for-debugging">Disable JIT for Debugging</a></li>
<li><a class="reference internal" href="#inspecting-code">Inspecting Code</a></li>
<li><a class="reference internal" href="#interpreting-graphs">Interpreting Graphs</a></li>
<li><a class="reference internal" href="#tracer">Tracer</a><ul>
<li><a class="reference internal" href="#tracing-edge-cases">Tracing Edge Cases</a></li>
<li><a class="reference internal" href="#automatic-trace-checking">Automatic Trace Checking</a></li>
<li><a class="reference internal" href="#tracer-warnings">Tracer Warnings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id1">Built-in Functions and Modules</a></li>
</ul>
</li>
<li><a class="reference internal" href="#frequently-asked-questions">Frequently Asked Questions</a></li>
<li><a class="reference internal" href="#appendix">Appendix</a><ul>
<li><a class="reference internal" href="#migrating-to-pytorch-1-2-recursive-scripting-api">Migrating to PyTorch 1.2 Recursive Scripting API</a><ul>
<li><a class="reference internal" href="#modules">Modules</a></li>
<li><a class="reference internal" href="#functions">Functions</a></li>
<li><a class="reference internal" href="#torchscript-classes">TorchScript Classes</a></li>
<li><a class="reference internal" href="#attributes">Attributes</a></li>
<li><a class="reference internal" href="#constants">Constants</a></li>
<li><a class="reference internal" href="#variables">Variables</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

            </div>
          </div>
        </div>
      </section>
    </div>

  


  

     
       <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
         <script src="_static/jquery.js"></script>
         <script src="_static/underscore.js"></script>
         <script src="_static/doctools.js"></script>
         <script src="_static/language_data.js"></script>
     

  

  <script type="text/javascript" src="_static/js/vendor/popper.min.js"></script>
  <script type="text/javascript" src="_static/js/vendor/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
  <script type="text/javascript" src="_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>
 
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-90545585-1', 'auto');
  ga('send', 'pageview');

</script>

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-117752657-2"></script>

<script>
  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());
  gtag('config', 'UA-117752657-2');
</script>

<img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/795629140/?label=txkmCPmdtosBENSssfsC&amp;guid=ON&amp;script=0"/>


  <!-- Begin Footer -->

  <div class="container-fluid docs-tutorials-resources" id="docs-tutorials-resources">
    <div class="container">
      <div class="row">
        <div class="col-md-4 text-center">
          <h2>Docs</h2>
          <p>Access comprehensive developer documentation for PyTorch</p>
          <a class="with-right-arrow" href="https://pytorch.org/docs/stable/index.html">View Docs</a>
        </div>

        <div class="col-md-4 text-center">
          <h2>Tutorials</h2>
          <p>Get in-depth tutorials for beginners and advanced developers</p>
          <a class="with-right-arrow" href="https://pytorch.org/tutorials">View Tutorials</a>
        </div>

        <div class="col-md-4 text-center">
          <h2>Resources</h2>
          <p>Find development resources and get your questions answered</p>
          <a class="with-right-arrow" href="https://pytorch.org/resources">View Resources</a>
        </div>
      </div>
    </div>
  </div>

  <footer class="site-footer">
    <div class="container footer-container">
      <div class="footer-logo-wrapper">
        <a href="https://pytorch.org/" class="footer-logo"></a>
      </div>

      <div class="footer-links-wrapper">
        <div class="footer-links-col">
          <ul>
            <li class="list-title"><a href="https://pytorch.org/">PyTorch</a></li>
            <li><a href="https://pytorch.org/get-started">Get Started</a></li>
            <li><a href="https://pytorch.org/features">Features</a></li>
            <li><a href="https://pytorch.org/ecosystem">Ecosystem</a></li>
            <li><a href="https://pytorch.org/blog/">Blog</a></li>
            <li><a href="https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md">Contributing</a></li>
          </ul>
        </div>

        <div class="footer-links-col">
          <ul>
            <li class="list-title"><a href="https://pytorch.org/resources">Resources</a></li>
            <li><a href="https://pytorch.org/tutorials">Tutorials</a></li>
            <li><a href="https://pytorch.org/docs/stable/index.html">Docs</a></li>
            <li><a href="https://discuss.pytorch.org" target="_blank">Discuss</a></li>
            <li><a href="https://github.com/pytorch/pytorch/issues" target="_blank">Github Issues</a></li>
            <li><a href="https://pytorch.org/assets/brand-guidelines/PyTorch-Brand-Guidelines.pdf" target="_blank">Brand Guidelines</a></li>
          </ul>
        </div>

        <div class="footer-links-col follow-us-col">
          <ul>
            <li class="list-title">Stay Connected</li>
            <li>
              <div id="mc_embed_signup">
                <form
                  action="https://twitter.us14.list-manage.com/subscribe/post?u=75419c71fe0a935e53dfa4a3f&id=91d0dccd39"
                  method="post"
                  id="mc-embedded-subscribe-form"
                  name="mc-embedded-subscribe-form"
                  class="email-subscribe-form validate"
                  target="_blank"
                  novalidate>
                  <div id="mc_embed_signup_scroll" class="email-subscribe-form-fields-wrapper">
                    <div class="mc-field-group">
                      <label for="mce-EMAIL" style="display:none;">Email Address</label>
                      <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Email Address">
                    </div>

                    <div id="mce-responses" class="clear">
                      <div class="response" id="mce-error-response" style="display:none"></div>
                      <div class="response" id="mce-success-response" style="display:none"></div>
                    </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->

                    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_75419c71fe0a935e53dfa4a3f_91d0dccd39" tabindex="-1" value=""></div>

                    <div class="clear">
                      <input type="submit" value="" name="subscribe" id="mc-embedded-subscribe" class="button email-subscribe-button">
                    </div>
                  </div>
                </form>
              </div>

            </li>
          </ul>

          <div class="footer-social-icons">
            <a href="https://www.facebook.com/pytorch" target="_blank" class="facebook"></a>
            <a href="https://twitter.com/pytorch" target="_blank" class="twitter"></a>
            <a href="https://www.youtube.com/pytorch" target="_blank" class="youtube"></a>
          </div>
        </div>
      </div>
    </div>
  </footer>

  <div class="cookie-banner-wrapper">
  <div class="container">
    <p class="gdpr-notice">To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: <a href="https://www.facebook.com/policies/cookies/">Cookies Policy</a>.</p>
    <img class="close-button" src="_static/images/pytorch-x.svg">
  </div>
</div>

  <!-- End Footer -->

  <!-- Begin Mobile Menu -->

  <div class="mobile-main-menu">
    <div class="container-fluid">
      <div class="container">
        <div class="mobile-main-menu-header-container">
          <a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
          <a class="main-menu-close-button" href="#" data-behavior="close-mobile-menu"></a>
        </div>
      </div>
    </div>

    <div class="mobile-main-menu-links-container">
      <div class="main-menu">
        <ul>
          <li>
            <a href="https://pytorch.org/get-started">Get Started</a>
          </li>

          <li>
            <a href="https://pytorch.org/features">Features</a>
          </li>

          <li>
            <a href="https://pytorch.org/ecosystem">Ecosystem</a>
          </li>

          <li>
            <a href="https://pytorch.org/mobile">Mobile</a>
          </li>

          <li>
            <a href="https://pytorch.org/hub">PyTorch Hub</a>
          </li>

          <li>
            <a href="https://pytorch.org/blog/">Blog</a>
          </li>

          <li>
            <a href="https://pytorch.org/tutorials">Tutorials</a>
          </li>

          <li class="active">
            <a href="https://pytorch.org/docs/stable/index.html">Docs</a>
          </li>

          <li>
            <a href="https://pytorch.org/resources">Resources</a>
          </li>

          <li>
            <a href="https://github.com/pytorch/pytorch">Github</a>
          </li>
        </ul>
      </div>
    </div>
  </div>

  <!-- End Mobile Menu -->

  <script type="text/javascript" src="_static/js/vendor/anchor.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function() {
      mobileMenu.bind();
      mobileTOC.bind();
      pytorchAnchors.bind();
      sideMenus.bind();
      scrollToAnchor.bind();
      highlightNavigation.bind();
      mainMenuDropdown.bind();
      filterTags.bind();

      // Remove any empty p tags that Sphinx adds
      $("[data-tags='null']").remove();

      // Add class to links that have code blocks, since we cannot create links in code blocks
      $("article.pytorch-article a span.pre").each(function(e) {
        $(this).closest("a").addClass("has-code");
      });
    })
  </script>
</body>
</html>